Try

sealed class Try<out T>

Representation of an operation that might successfully return a value or throw an exception.

An instance of Try may be either a Success or a Failure.

This type is based on scala.util.Try.

Note: Only non-fatal exceptions are caught by the combinators on Try (see NonFatal). Serious system errors, on the other hand, will be thrown.

Parameters

T

Type of the value of a successful operation.

Types

Companion
Link copied to clipboard
common
object Companion

Functions

filter
Link copied to clipboard
common
abstract fun filter(predicate: (T) -> Boolean): Try<T>
Returns the same Success if the predicate is satisfied for the value.
filterIsInstance
Link copied to clipboard
common
inline fun <R> filterIsInstance(): Try<R>
Returns the same Success casted to type R if it is R.
filterNot
Link copied to clipboard
common
abstract fun filterNot(predicate: (T) -> Boolean): Try<T>
Returns the same Success if the predicate is not satisfied for the value.
filterOrElse
Link copied to clipboard
common
abstract fun filterOrElse(predicate: (T) -> Boolean, throwable: (T) -> Throwable): Try<T>
Returns the same Success if the predicate is satisfied for the value.
flatMap
Link copied to clipboard
common
abstract fun <R> flatMap(transform: (T) -> Try<R>): Try<R>
Maps value of a Success to a new Try using transform or returns the same Try if this is a Failure.
fold
Link copied to clipboard
common
inline fun <R> fold(successTransform: (T) -> R, failureTransform: (Throwable) -> R): R
Transforms a Success using successTransform or a Failure using failureTransform.
forEach
Link copied to clipboard
common
abstract fun forEach(action: (T) -> Unit)
Runs action if this is a Success.
get
Link copied to clipboard
common
abstract fun get(): T
Gets the value of a Success or throw an exception from a Failure.
getOrNull
Link copied to clipboard
common
abstract fun getOrNull(): T?
Gets the value of a Success or null if this is a Failure.
map
Link copied to clipboard
common
abstract fun <R> map(transform: (T) -> R): Try<R>
Maps value of a Success using transform or returns the same Try if this is a Failure.
toEither
Link copied to clipboard
common
abstract fun toEither(): Either<Throwable, T>
Converts this Try to Either.
toOption
Link copied to clipboard
common
abstract fun toOption(): Option<T>
Converts this Try to Option.
transform
Link copied to clipboard
common
inline fun <R> transform(successTransform: (T) -> Try<R>, failureTransform: (Throwable) -> Try<R>): Try<R>
Transforms a Success using successTransform or a Failure using failureTransform.
zip
Link copied to clipboard
common
infix fun <R> zip(other: Try<R>): Try<Pair<T, R>>
Returns Success containing a Pair of values of this and other if both instances of Try are Success.
inline fun <T1, R> zip(other: Try<T1>, transform: (T, T1) -> R): Try<R>
Returns Success containing the result of applying transform to both values of this and other if both instances of Try are Success.

Properties

failed
Link copied to clipboard
common
abstract val failed: Try<Throwable>
Returns a Success with an exception it this is a Failure or a Failure if this is a Success.
isFailure
Link copied to clipboard
common
abstract val isFailure: Boolean
Returns true if this is a Failure or false if this is Success.
isSuccess
Link copied to clipboard
common
abstract val isSuccess: Boolean
Returns true if this is a Success or false if this is Failure.

Inheritors

Success
Link copied to clipboard
Failure
Link copied to clipboard

Extensions

evert
Link copied to clipboard
common
fun <T> Try<Option<T>>.evert(): Option<Try<T>>
Moves inner Option outside of the outer Try.
filterNotNull
Link copied to clipboard
common
fun <T> Try<T?>.filterNotNull(): Try<T>
Returns the same Success if its value is not null.
flatten
Link copied to clipboard
common
fun <T> Try<Option<T>>.flatten(): Option<T>
Extracts an Option nested in the Try to a not nested Option.
fun <T> Try<Try<T>>.flatten(): Try<T>
Transforms a nested Try to a not nested Try.
getOrElse
Link copied to clipboard
common
inline fun <T> Try<T>.getOrElse(default: () -> T): T
Gets the value of a Success or default value if this is a Failure.
orElse
Link copied to clipboard
common
inline fun <T> Try<T>.orElse(default: () -> Try<T>): Try<T>
Returns this Try if this is a Success or default if this is a Failure.
recover
Link copied to clipboard
common
inline fun <T> Try<T>.recover(rescue: (Throwable) -> T): Try<T>
Returns this Try if this is a Success or a Try created for the rescue operation if this is a Failure.
recoverWith
Link copied to clipboard
common
inline fun <T> Try<T>.recoverWith(rescue: (Throwable) -> Try<T>): Try<T>
Returns this Try if this is a Success or a Try created by the rescue function if this is a Failure.